package com.techsoul.admin.controller;

import cn.hutool.core.collection.CollUtil;
import com.techsoul.admin.pojo.param.TmsContractParams;
import com.techsoul.common.core.pojo.Result;
import com.techsoul.module.contract.service.IConContractService;
import com.techsoul.module.goods.pojo.param.BrandSearchParam;
import com.techsoul.module.goods.service.IBrandService;
import com.techsoul.module.tms.pojo.param.TFeeRoutePriceNewSaveParam;
import com.techsoul.module.tms.service.ITFeeRoutePriceNewService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * tms合同 控制器
 *
 */
@Api(tags = "合同")
@RestController
@RequestMapping("/tms/contract")
public class TmsContactController {

    @Autowired
    private Result result;

    @Autowired
    private IConContractService contractService;

    @Autowired
    private ITFeeRoutePriceNewService feeRoutePriceNewService;

    @ApiOperation("新增")
    @PostMapping("/insert")
    public Result insert(@RequestBody TmsContractParams params) {
        String contractId = contractService.insert(params);
        if (CollUtil.isNotEmpty(params.getRoutePriceNewList())) {
            for (TFeeRoutePriceNewSaveParam tFeeRoutePriceNewSaveParam : params.getRoutePriceNewList()) {
                tFeeRoutePriceNewSaveParam.setContractId(contractId);
                feeRoutePriceNewService.insert(tFeeRoutePriceNewSaveParam);
            }
        }
        return result.success(contractId);
    }

    @ApiOperation("更新")
    @PostMapping("/update")
    public Result update(@RequestBody TmsContractParams params) {
        contractService.update(params);
        // 明细全删全入
        feeRoutePriceNewService.deleteByContractId(params.getId());
        if (CollUtil.isNotEmpty(params.getRoutePriceNewList())) {
            for (TFeeRoutePriceNewSaveParam tFeeRoutePriceNewSaveParam : params.getRoutePriceNewList()) {
                tFeeRoutePriceNewSaveParam.setContractId(params.getId());
                feeRoutePriceNewService.insert(tFeeRoutePriceNewSaveParam);
            }
        }
        return result.success();
    }

}
